/* 
 * Javascript for sqpoll.
 * @author Younggue Bae 
 */


/**
 * Returns true if a value is empty.
 */
function isEmpty(val) {
    return (val === undefined || val == null || val.length <= 0) ? true : false;
}

function replaceAll(str, find, replacement) {
	return str.split(find).join(replacement);
}

/**
 * Adds days to the date.
 * 
 * @param date
 * @param day
 * @returns
 */
function addDay(date, days) {
	var dt = new Date(date.setDate(date.getDate() + days));
	
	//return dt.toLocaleDateString();
	return dt;
}

function formatStringDate(str, format) {
	var date = new Date();
	var fields = str.split("-");
	
	if (fields.length == 3) {
		var year = fields[0];
		var month = fields[1];
		var ddhhmmss = fields[2].split("T");
		
		date.setYear(year);
		date.setMonth(month - 1);
		
		if (ddhhmmss.length == 1) {
			var dd = ddhhmmss[0];
			date.setDate(dd);
		}
		else if (ddhhmmss.length == 2) {
			var dd = ddhhmmss[0];
			date.setDate(dd);
			
			var hhmmss = ddhhmmss[1].split(":");
			if (hhmmss.length == 3) {
				var hours = hhmmss[0];
				var minutes = hhmmss[1];
				var seconds = hhmmss[2];
				
				date.setHours(hours);
				date.setMinutes(minutes);
				date.setSeconds(seconds);
			}
		}
	}
	
	return formatDate(date, format);
}

function formatDate(date, format) {
	var month = date.getMonth() + 1;
	var day = date.getDate();
	var year = date.getFullYear();
	
	if (month < 10) {
		month = '0' + month;
	}
	
	if (day < 10) {
		month = '0' + day;
	}
	
	var dt = date.toLocaleDateString();
	if (format == "yyyy-MM-dd") {
		dt = year + "-" + month + "-" + day;
	}
	else if (format == "yyyy/MM/dd") {
		dt = year + "/" + month + "/" + day;
	}
	else if (format == "MM-dd") {
		dt = month + "-" + day;
	}
	else if (format == "MM.dd") {
		dt = month + "." + day;
	}
	else if (format == "MM/dd") {
		dt = month + "/" + day;
	}
	
	return dt;
}

